package com.jason.practice.service.impl;

import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.jason.practice.model.CitizenLoanInfo;
import com.jason.practice.model.InsuranceProductInfo;
import com.jason.practice.mapper.InsuranceProductInfoMapper;
import com.jason.practice.response.Response;
import com.jason.practice.service.InsuranceProductInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jason.practice.utils.ExcelUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.stereotype.Service;

import java.io.File;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jiayb_9169@126.com
 * @since 2022-10-23
 */
@Service
public class InsuranceProductInfoServiceImpl extends ServiceImpl<InsuranceProductInfoMapper, InsuranceProductInfo> implements InsuranceProductInfoService {
    @Override
    public Response uploadExcel(File file) {
        List<InsuranceProductInfo> t0079JList = new ArrayList<>();
        try {
            ImportParams im = new ImportParams();
            im.setTitleRows(3);
            im.setHeadRows(2);
            im.setStartSheetIndex(1);
            im.setReadRows(7);
            t0079JList = ExcelUtil.parseExcel(file, InsuranceProductInfo.class, im);
            int insuranceProductNum = 0;
            int teamInsuredEnterpriseNum = 0;
            int teamInsuredHumanNum = 0;
            int insuredHumanNum = 0;
            BigDecimal accPremiumIncome =  new BigDecimal(0.00);
            BigDecimal insurancePrice =  new BigDecimal(0.00);
            BigDecimal accPayPrice =  new BigDecimal(0.00);
            for (InsuranceProductInfo t0079J : t0079JList) {
                t0079J.setExcelName(file.getName());
                t0079J.setSheetName("支持新市民保险产品情况统计表");
                if (!StringUtils.isBlank(t0079J.getInsuranceProductNum())) {
                    insuranceProductNum += NumberUtils.toInt(t0079J.getInsuranceProductNum());
                }
                if (!StringUtils.isBlank(t0079J.getTeamInsuredEnterpriseNum())) {
                    teamInsuredEnterpriseNum += NumberUtils.toInt(t0079J.getTeamInsuredEnterpriseNum());
                }
                if (!StringUtils.isBlank(t0079J.getTeamInsuredHumanNum())) {
                    teamInsuredHumanNum += NumberUtils.toInt(t0079J.getTeamInsuredHumanNum());
                }
                if (!StringUtils.isBlank(t0079J.getInsuredHumanNum())) {
                    insuredHumanNum += NumberUtils.toInt(t0079J.getInsuredHumanNum());
                }
                if (!StringUtils.isBlank(t0079J.getAccPremiumIncome())) {
                    BigDecimal bd = new BigDecimal(t0079J.getAccPremiumIncome());
                    accPremiumIncome = accPremiumIncome.add(new BigDecimal(bd.toString()));
                }
                if (!StringUtils.isBlank(t0079J.getInsurancePrice())) {
                    BigDecimal bd = new BigDecimal(t0079J.getInsurancePrice());
                    insurancePrice = insurancePrice.add(new BigDecimal(bd.toString()));
                }
                if (!StringUtils.isBlank(t0079J.getAccPayPrice())) {
                    BigDecimal bd = new BigDecimal(t0079J.getAccPayPrice());
                    accPayPrice = accPayPrice.add(new BigDecimal(bd.toString()));
                }
            }
            InsuranceProductInfo total = new InsuranceProductInfo();
            total.setInsuranceProductNum(insuranceProductNum+"");
            total.setTeamInsuredEnterpriseNum(teamInsuredEnterpriseNum+"");
            total.setTeamInsuredHumanNum(teamInsuredHumanNum+"");
            total.setInsuredHumanNum(insuredHumanNum+"");

            total.setAccPremiumIncome(accPremiumIncome.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue()+"");
            total.setInsurancePrice(insurancePrice.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue()+"");
            total.setAccPayPrice(accPayPrice.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue()+"");

            ExcelUtil.editExcel(file, total);

//            saveBatch(t0079JList);
        } catch (Exception e) {
            log.error("BaseCommonServiceImpl error:", e);
        }
        return Response.ok(t0079JList);
    }
}
